package com.wentao.mall.controller;

import com.wentao.mall.commons.Constant;
import com.wentao.mall.commons.Result;
import com.wentao.mall.filter.CustomerFilter;
import com.wentao.mall.model.po.User;
import com.wentao.mall.model.vo.CartVo;
import com.wentao.mall.service.CartService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import javax.validation.constraints.Min;
import java.util.List;

/**
 * 购物车控制器
 */
@Validated
@RestController
public class CartController {
    @Autowired
    private CartService cartService;

    @ApiOperation("用户购物车列表")
    @GetMapping("/cart/list")
    public Result cartList(HttpSession httpSession){
        User user =(User) httpSession.getAttribute(Constant.CURRENT_USER);
        List<CartVo> cartVoList=cartService.cartList(user.getId());
        System.out.println(cartVoList.toString());
        return Result.success(cartVoList);
    }

    @ApiOperation("添加商品到购物车")
    @PostMapping("/cart/add")
    public Result addCart(@RequestParam("productId") Integer productId, @RequestParam("count") @Min(1) Integer count){
        User user = CustomerFilter.currentUser;
        //addCart
        cartService.addCart(productId,count,user);
        //回显购物车列表
        List<CartVo> cartVoList=cartService.cartList(user.getId());
        System.out.println(cartVoList.toString());
        return Result.success(cartVoList);
    }

    @ApiOperation("更新购物车某商品数量")
    @PostMapping("/cart/update")
    public Result updateCart(@RequestParam("productId") Integer productId, @RequestParam("count") @Min(1) Integer count){
        User user = CustomerFilter.currentUser;
        cartService.updateCart(productId,count,user);
        List<CartVo> cartVoList=cartService.cartList(user.getId());
        System.out.println(cartVoList.toString());
        return Result.success(cartVoList);
    }

    @ApiOperation("删除购物车中某商品")
    @PostMapping("/cart/delete")
    public Result deleteCart(@RequestParam("productId") Integer productId){
        User user = CustomerFilter.currentUser;
        cartService.deleteCart(productId,user);
        List<CartVo> cartVoList=cartService.cartList(user.getId());
        System.out.println(cartVoList.toString());
        return Result.success(cartVoList);
    }

    @ApiOperation("选中/不选中购物车商品")
    @PostMapping("/cart/select")
    public Result selectedCart(@RequestParam("productId") Integer productId,@RequestParam(value = "selected",defaultValue = "1") Integer selected){
        User user = CustomerFilter.currentUser;
        cartService.selectedCart(productId,selected,user);
        List<CartVo> cartVoList=cartService.cartList(user.getId());
        System.out.println(cartVoList.toString());
        return Result.success(cartVoList);
    }

    @ApiOperation("全选/全不选购物车的某个商品")
    @PostMapping("/cart/selectAll")
    public Result selectAll(@RequestParam("selected") Integer selected){
        User user = CustomerFilter.currentUser;
        cartService.selectedAll(selected,user);
        List<CartVo> cartVoList=cartService.cartList(user.getId());
        System.out.println(cartVoList.toString());
        return Result.success(cartVoList);
    }
}
